﻿package ua.mihaylenko.cardsplugin.components.cardeditor
{
	import fl.controls.ComboBox;
	import fl.data.DataProvider;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import ua.mihaylenko.cardsplugin.common.EngineMain;
	import ua.mihaylenko.cardsplugin.common.DocumentClass;
	public class ECEFild extends MovieClip
	{
		public var _stxValue:TextField;
		public var _stxValues:ComboBox;
		public var _stxName:TextField;
		public var _smcBackground:MovieClip;
		//public var _leftDrag:Sprite;
		//public var _rightDrag:Sprite;
		private var _curTarget:Sprite;
		private var _isDrag:Boolean = false;
		public function ECEFild()
		{
			_stxValues.visible = false;
			_stxValues.tabEnabled = false;
			_stxValues.tabChildren = false;
			_stxValue.text = "";
			_stxValue.tabEnabled = true;
			_stxName.text = "";
			_stxName.mouseEnabled = false;
			_smcBackground.mouseChildren = false;
			//Drag Handler for Issue 57
			//_leftDrag.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			//_leftDrag.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			//_leftDrag.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);			
			//_rightDrag.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			//_rightDrag.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			//_rightDrag.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		public function set multiline(bMulti:Boolean)
		{
			if (bMulti)
			{
				_stxValue.multiline = true;
				_stxValue.height *= 2;
				_smcBackground.height = _stxValue.height;
			}
		}
		public function get multiline():Boolean
		{
			return _stxValue.multiline;
		}
		
		public function set values(sValues:Array)
		{
			if (sValues.length > 0)
			{
				_stxValues.visible = true;
				_stxValue.visible = false;
				_stxValues.dataProvider = new DataProvider(sValues);
			}
			else
			{
				_stxValue.visible = true;
				_stxValues.visible = false;
			}
		}
		
		public function set value(sValue:String)
		{
			if (_stxValues.visible)
			{
				for (var i:uint; i < _stxValues.length; i++)
				{
					if (_stxValues.getItemAt(i).label == sValue)
					{
						_stxValues.selectedIndex = i;
						break;
					}
				}				
				return;
			}
			_stxValue.text = sValue;
		}
		public function get value():String
		{
			if (_stxValues.visible)
			{
				return _stxValues.selectedLabel;
			}
			return _stxValue.text;
		}
		public function set maxChars(iMaxChar:int)
		{
			if (iMaxChar)
			{
				_stxValue.maxChars = iMaxChar;
			}
		}
		public function set title(sTitle:String)
		{
			_stxName.text = sTitle;
		}
		public function get title():String
		{
			return _stxName.text;
		}
		
		public function getHeight():uint
		{
			if (_stxValues.visible)
			{
				return 40;
			}
			else
			{
				return 20 + _stxValue.height;
			}
		}
		public function setWidth(width:uint):void
		{
			if (width >= 1)
			{
				_stxValue.width = width;
				_stxValues.width = width - 10;
				_smcBackground.width = width;
			}
		}
		
		/*
		public function mouseOverHandler(event:MouseEvent):void
		{
			Mouse.hide();
			_curTarget = event.currentTarget as Sprite;
			_curTarget.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			DocumentClass.AppStage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
		}
		
		private function mouseMoveHandler(event:MouseEvent):void
		{
            EngineMain.instance._cursor.x = event.stageX;
            EngineMain.instance._cursor.y = event.stageY;
            event.updateAfterEvent();
            EngineMain.instance._cursor.visible = true;
			var width:int = _rightDrag.x - _leftDrag.x;
			if (width > 1)
			{
				_stxValue.width = width - 3;
				_stxValues.width = width - 3;
				_smcBackground.width = width - 2;
				_smcBackground.x = _leftDrag.x + 1;
				_stxValue.x = _leftDrag.x + 1;
				_stxValues.x = _leftDrag.x + 2;
			}
        }
		
		public function mouseOutHandler(event:MouseEvent):void
		{			
			Mouse.show();
			if(_curTarget) _curTarget.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			EngineMain.instance._cursor.visible = false;
		}
		private function mouseLeaveHandler(event:Event):void
		{
			mouseMoveHandler(new MouseEvent(MouseEvent.MOUSE_MOVE));
            mouseOutHandler(new MouseEvent(MouseEvent.MOUSE_MOVE));
			DocumentClass.AppStage.removeEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
        }
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			_isDrag = true;
			_curTarget.startDrag(false, new Rectangle( -5, 23, 177, 0));
			DocumentClass.AppStage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function mouseUpHandler(event:MouseEvent):void
		{
			_isDrag = false;
			_curTarget.stopDrag();
			mouseMoveHandler(new MouseEvent(MouseEvent.MOUSE_MOVE));
			DocumentClass.AppStage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		*/
	}
}